﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL;
using UCL.Data;
using UCL.Forms;

namespace UAS5
{
    /// <summary>
    /// 2013-6-6 by sutroon
    /// </summary>
    public partial class frmTagList : Form
    {
        AppDataHelper adh = new AppDataHelper();
        private int ItemID;
        public frmTagList()
        {
            InitializeComponent();
        }

        private void frmTagList_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        public void LoadData()
        {
            lstTags.DataSource = adh.ExecuteDataTable("select ID,Name from tgeneralcatalog where SiteTag='quickdial' order by Ordinal");
            lstTags.DisplayMember = "Name";
            lstTags.ValueMember = "ID";
            lstTags.SelectedIndex = -1;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.RefreshOwner<frmMain>();
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataModule dm = new DataModule("tgeneralcatalog", adh);
            dm.AddParam("Name", tbName.Text, "名称未填写");
            dm.AddParam("Content", cbbContent.SelectedItem,"图片类型未选择");
            bool result;
            if (ItemID == 0)
            {
                dm.AddParam("Ordinal", 0, "", "int");
                dm.AddParam("SiteTag", "quickdial");
                result = dm.Insert();
            }
            else
            {
                result = dm.Update("ID=" + ItemID);
            }
            MessageBox.Show(result?"保存成功":dm.GetError());
            if (result)
            {
                LoadData();
                tbName.Clear();
                ItemID = 0;
            }
        }

        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstTags.SelectedItem == null) return;
            DataRowView r = (DataRowView)lstTags.SelectedItem;
            DataTable dt = adh.ExecuteDataTable("select ID,Name,Content from tgeneralcatalog where ID=" + r["ID"]);
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("记录不存在!");
                return;
            }
            ItemID = int.Parse(dt.Rows[0]["ID"].ToString());
            tbName.Text = dt.Rows[0]["Name"].ToString();
            cbbContent.SelectedItem = dt.Rows[0]["Content"].ToString();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstTags.SelectedItem == null) return;
            DataRowView r = (DataRowView)lstTags.SelectedItem;
            int count=adh.ExecuteDataTable(string.Format("select count(0) from tquickdial where Tags='{0}'",r["Name"])).Rows[0][0].ToString().ToInt();
            if (count > 0)
            {
                if (DialogResult.OK != MessageBox.Show(string.Format("此标签下有{0}个项目，您必需清空所有项目才能删除标签！\n您是否确定删除所有项目并删除此标签？",count), "提示", MessageBoxButtons.OKCancel)) return;
                adh.Delete("tquickdial", string.Format("Tags='{0}'",r["Name"]));
            }
            adh.Delete("tgeneralcatalog", "ID=" + r["ID"]);
            LoadData();
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ItemID = 0;
            tbName.Clear();
        }

        private void 次序上移ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstTags.SelectedItem == null) return;
            DataRowView r = (DataRowView)lstTags.SelectedItem;
            int i = lstTags.SelectedIndex;
            if (i == 0) return;
            DataRowView rPrev = (DataRowView)lstTags.Items[i - 1];
            int ordinal = adh.ExecuteDataTable("select Ordinal from tgeneralcatalog where ID=" + rPrev["ID"]).Rows[0][0].ToString().ToInt();

            adh.Update("tgeneralcatalog", "Ordinal="+(ordinal+1), "ID=" + rPrev["ID"]);
            adh.Update("tgeneralcatalog", "Ordinal="+ordinal, "ID=" + r["ID"]);
            LoadData();
        }

        private void 次序下移ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstTags.SelectedItem == null) return;
            DataRowView r = (DataRowView)lstTags.SelectedItem;
            int i = lstTags.SelectedIndex;
            if (i == lstTags.Items.Count) return;
            DataRowView rNext = (DataRowView)lstTags.Items[i + 1];
            int ordinal = adh.ExecuteDataTable("select Ordinal from tgeneralcatalog where ID=" + rNext["ID"]).Rows[0][0].ToString().ToInt();

            adh.Update("tgeneralcatalog", "Ordinal=" + (ordinal - 1), "ID=" + rNext["ID"]);
            adh.Update("tgeneralcatalog", "Ordinal=" + ordinal, "ID=" + r["ID"]);
            LoadData();
        }
    }
}
